<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!showAddress" class="none">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li><span>收<i />货<i />人：</span>{{ showAddress.receiver }}</li>
        <li><span>联系方式：</span>{{ showAddress.contact.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') }}</li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation }}{{ showAddress.address }}</li>
      </ul>
      <a v-if="showAddress" @click="openEditAddress(showAddress)" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <XtxButton @click="openDialog" v-if="showAddress" class="btn">切换地址</XtxButton>
      <XtxButton @click="openEditAddress({})" class="btn">添加地址</XtxButton>
    </div>
    <!-- 添加收货地址的组件-->
    <AddressEdit @edit-address="editAddress" ref="addressEditCom"></AddressEdit>
    <!-- 切换收货地址的对话框 -->
    <xtx-dialog title="切换收货地址" v-model:visible="dialogVisible">
      <div @click="selectedAddress = item" class="text item" :class="{ active: item.id === selectedAddress?.id }"
        v-for="item in address" :key="item.id">
        <ul>
          <li><span>收<i />货<i />人：</span>{{ item.receiver }}</li>
          <li><span>联系方式：</span>{{ item.contact }}</li>
          <li><span>收货地址：</span>{{ item.fullLocation + item.address }}</li>
        </ul>
      </div>
      <template #footer>
        <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton @click="confirmAddress" type="primary">确认</XtxButton>
      </template>
    </xtx-dialog>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  name: 'CheckoutAddress',
  components: { AddressEdit },
  props: {
    address: {
      type: Array,
      default: () => []
    }
  },
  setup(props, { emit }) {
    // 显示的地址
    const showAddress = ref(null)
    // 组件初始化时，判断用户有没有地址，如果有地址，再判断有没有默认地址，如果有默认地址就取默认地址显示，如果没有默认地址，就取数组的第1项
    watch(() => props.address, () => {
      if (props.address.length) {
        const defaultAddress = props.address.find(item => item.isDefault === 0)
        if (defaultAddress) {
          showAddress.value = defaultAddress
        } else {
          // 这种修改也会修改props的数据，违背props数据单向修改的原则，所以eslint报警告错误，可以采用添加以下注释的方式清除警告
          // eslint-disable-next-line vue/no-setup-props-destructure
          showAddress.value = props.address[0]
        }
      }
    }, { immediate: true, deep: true })

    // 组件初始化传递当前的收货地址id给父组件
    emit('on-success', showAddress.value?.id)

    // 控制切换地址对话框的显示和隐藏
    const dialogVisible = ref(false)
    const selectedAddress = ref(null)
    // 打开切换收货对话框
    const openDialog = () => {
      selectedAddress.value = null
      dialogVisible.value = true
    }
    // 提交切换地址
    const confirmAddress = () => {
      showAddress.value = selectedAddress.value
      dialogVisible.value = false
      // 把地址的id传递到父组件中，将来提交订单时需要收货人地址的id
      emit('on-success', selectedAddress.value.id)
    }

    // 增加/修改收货地址
    // 获取添加/修改的组件对象
    const addressEditCom = ref(null)
    const openEditAddress = (showAddress) => {
      // 调用组件对象中的open方法
      addressEditCom.value.open(showAddress)
    }
    // 接收添加/修改后的地址
    const editAddress = (newAddress) => {
      // 判断是添加还是修改
      const hasAddress = props.address.find(item => item.id === newAddress.id)
      if (hasAddress) {
        // 都要把i地址id传递到父组件中
         emit('on-success', hasAddress.id)
        for (const key in hasAddress) {
          hasAddress[key] = newAddress[key]
        }
      } else {
         // 都要把i地址id传递到父组件中
         emit('on-success', newAddress.id)
        // 添加：追加到原地址数组的最前面
       // console.log(newAddress) // Proxy()
        const json = JSON.stringify(newAddress) // 需要克隆下，不然使用的是对象的引用
        // eslint-disable-next-line vue/no-mutating-props
        props.address.unshift(JSON.parse(json))
      }
    }

    return { showAddress, dialogVisible, selectedAddress, confirmAddress, openDialog, openEditAddress, addressEditCom, editAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;

    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;

      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }

      >ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}

.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;

  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;

    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }

    >ul {
      flex: 1;
      padding: 20px;

      li {
        line-height: 30px;

        span {
          color: #999;
          margin-right: 5px;

          >i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }

    >a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }

  .action {
    width: 420px;
    text-align: center;

    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;

      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>